package 剑指offer;

/**
 * @description: 给定一个十进制数M，以及需要转换的进制数N。将十进制数M转化为N进制数
 * @author: ywk
 * @date: 2020-11-07
 */
public class 进制转换 {
    public static void main(String[] args) {
        System.out.println(solve(23, 12));

    }

    /**
     * 7,2
     * 111
     *
     * @param M
     * @param N
     * @return
     */
    public static String solve(int M, int N) {
        // write code here
        String[] arr = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F".split(",");
        StringBuilder sb = new StringBuilder();
        String pre = "";
        if (M < 0) {
            pre = "-";
            M = -M;
        }
        while (M != 0) {
            int reminder = M % N;
            M = M / N;
            sb.append(arr[reminder]);
        }
        return pre + sb.reverse();
    }
}
